package model_2_二叉树;

public class BinaryTreeNode {
    private int value;
    private BinaryTreeNode left;
    private BinaryTreeNode right;

    public BinaryTreeNode(int value) {
        this.value = value;
        left = null;
        right = null;
    }

    public int getValue() {
        return value;
    }

    public BinaryTreeNode getLeft() {
        return left;
    }

    public void setLeft(BinaryTreeNode left) {
        this.left = left;
    }

    public BinaryTreeNode getRight() {
        return right;
    }

    public void setRight(BinaryTreeNode right) {
        this.right = right;
    }

    public static void main(String[] args) {
        // 创建二叉树
        BinaryTreeNode root = new BinaryTreeNode(1);
        BinaryTreeNode node2 = new BinaryTreeNode(2);
        BinaryTreeNode node3 = new BinaryTreeNode(3);
        BinaryTreeNode node4 = new BinaryTreeNode(4);
        BinaryTreeNode node5 = new BinaryTreeNode(5);

        root.setLeft(node2);
        root.setRight(node3);
        node2.setLeft(node4);
        node2.setRight(node5);

        // 遍历二叉树（示例）
        System.out.println("前序遍历：");
        preorderTraversal(root);

        System.out.println("\n中序遍历：");
        inorderTraversal(root);

        System.out.println("\n后序遍历：");
        postorderTraversal(root);
    }

    // 前序遍历
    public static void preorderTraversal(BinaryTreeNode node) {
        if (node != null) {
            System.out.print(node.getValue() + " ");
            preorderTraversal(node.getLeft());
            preorderTraversal(node.getRight());
        }
    }

    // 中序遍历
    public static void inorderTraversal(BinaryTreeNode node) {
        if (node != null) {
            inorderTraversal(node.getLeft());
            System.out.print(node.getValue() + " ");
            inorderTraversal(node.getRight());
        }
    }

    // 后序遍历
    public static void postorderTraversal(BinaryTreeNode node) {
        if (node != null) {
            postorderTraversal(node.getLeft());
            postorderTraversal(node.getRight());
            System.out.print(node.getValue() + " ");
        }
    }
}
